package awesome.cpu.structure;

public class OrGate extends NonSequentialComponent {
	
	public final static int KEY_CONDITION_MET = 1;
	private int mConditionMet;
	public final static int KEY_PCWRITE = 2;
	private int mPCWrite;
	
	public final static int KEY_RESULT = 3;
	//private int mResult;
	
	private final static OrGate mInstance = new OrGate();
	
	private OrGate() {}
	
	public static OrGate getInstance() {
		return mInstance;
	}
	
	@Override
	boolean setData(int keyInterface, int data) {
		switch (keyInterface) {
		case KEY_CONDITION_MET:
			mConditionMet = data;
			return true;
		case KEY_PCWRITE:
			mPCWrite = data;
			return true;
		}
		return false;
	}

	@Override
	int getData(int keyInterface) {
		
		BusMonitor.transferData(Control.getInstance(), Control.KEY_PC_WRITE,
				this, KEY_PCWRITE);
		BusMonitor.transferData(BranchCondGates.getInstance(), BranchCondGates.KEY_RESULT,
				this, KEY_CONDITION_MET);
		
		switch (keyInterface) {
		case KEY_RESULT:
			return result();
		}
		return 0;
	}	
	
	private int result() {
		return (mConditionMet == 1 || mPCWrite == 1) ? 1 : 0;
	}
}